exports.successful_pay = function(req,res) {

    //获取restful参数
    var restfulParams = req.routeInfo.restfulParams;
    var resultData = {};
    var couponInfo = null;
    var couponData = null;
    var art_type;
    //任务流创建
    async.waterfall([
        //查询订单信息
        function(cb){

            R.SEND_HTTP(
                req,
                {
                    url    : CORE_APIConfig.coreAPI.search_order+"/o_no/" + restfulParams.o_no ,
                    method : 'POST',
                    data:{
                        fields:[
                            "o_oid", //机构id
                            "o_uid",
                            "o_type", //订单的类型 0-机构活动 1-论坛活动 2-课程\r\n
                            "o_activity_type",//如果为活动， 0-拼团 1-投票 2-砍价 3-H5海报 4-助力 5-促销活动 6-微传单
                            "o_no",  //订单编号
                            "o_aoc_id", //关联活动或者课程ID
                            "o_pay_type",    //支付类型  0-微信支付 1-余额支付
                            "o_pay_money"    //实际付款金额
                        ].join(",")
                    }
                },
                function (err, data) {

                    resultData.orderData = data.data;
                    console.log( resultData.orderData );
                    cb();
                }
            );

        },
        //查询该订单对应的课程或者活动的艺术类别
        function(cb){
        if(resultData.orderData[0].o_type == 2){
            ////console.log(resultData.orderData[0].o_aoc_id);
            R.SEND_HTTP(
                req,
                {
                    url    : CORE_APIConfig.coreAPI.search_course+"/oc_id/" + resultData.orderData[0].o_aoc_id,
                    method : 'POST',
                    data:{
                        fields:[
                            "oc_cid",
                        ].join(",")
                    }
                },
                function (err, data) {
                    //console.log(data.data)
                    art_type = data.data[0].oc_cid;
                    //console.log("art :" + art_type);
                    cb();
                }

            );

        }else{
            cb();
        }
        },
        function (cb) {
            if(resultData.orderData[0].o_type == 2){
            ////console.log("art_type" + art_type);
            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.search_course + "/oc_cid/" + art_type +"/limit/2",
                    method: 'POST',
                    data: {
                        fields: [
                            "oc_oid","oc_id", "oc_course_count", "oc_title", "oc_title_image", "oc_sales_volume", "oc_new_price"
                        ].join(",")
                    }
                },
                function (err, data) {

                    resultData.courseData = data.data;
                    console.log("********");
                    console.log(resultData.courseData);
                    console.log("********");
                    cb();
                }
            );
        }else{
                cb();
            }
        },
        //拼团
        function (cb) {
            if(resultData.orderData[0].o_type == 0 && resultData.orderData[0].o_activity_type == 0){
                console.log(132);
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_organization_activity + "/oa_type/" + resultData.orderData[0].o_activity_type+"/limit/2",
                        method: 'POST',
                        data: {
                            fields: [
                                "oa_id as oc_id", "oa_title as oc_title","oa_title_image as oc_title_image","oa_oid as oc_oid"
                            ].join(",")
                        }
                    },
                    function (err, data) {
                        ////console.log(data.data);
                        resultData.courseData = data.data;

                        cb();
                    }
                );

            }else if(resultData.orderData[0].o_type == 0 && resultData.orderData[0].o_activity_type == 5){
                
                console.log("促销活动...");

              console.log("1: "+couponData);
                //查询是否有代金劵奖励
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchDiscountsActivityConfig + "/dac_oaid/" + resultData.orderData[0].o_aoc_id,
                        method: 'POST'
                    },
                    function (err, data) {

                        if(err){
                            return cb();
                        }

                        if(data.data.length <= 0){
                            return cb();
                        }else{

                            couponData = data.data[0];

                            if(couponData.dac_coupon_id == 0){
                                return cb();
                            }else{

                                if(couponData.dac_coupon_type == 0){

                                    R.SEND_HTTP(
                                        req,
                                        {
                                            url: CORE_APIConfig.coreAPI.search_cash_coupon+"/cc_id/"+couponData.dac_coupon_id,
                                            method: 'GET',
            
                                        },
                                        function (err, data) {
                                            //判断是否有错误信息
                                            if (err) {
                                                return cb();
                                            }
        
                                            couponInfo = data.data[0];

                                            cb();
                                        }
                                    );
                                    
                                }else if(couponData.dac_coupon_type == 1){

                                    R.SEND_HTTP(
                                        req,
                                        {
                                            url: CORE_APIConfig.coreAPI.search_course_coupon+"/cc_id/"+couponData.dac_coupon_id,
                                            method: 'GET',
            
                                        },
                                        function (err, data) {
                                            //判断是否有错误信息
                                            if (err) {
                                                return cb();
                                            }
                                            couponInfo = data.data[0];
        
                                            cb();
            
                                        }
                                    );

                                }else if(couponData.dac_coupon_type == 2){

                                    R.SEND_HTTP(
                                        req,
                                        {
                                            url: CORE_APIConfig.coreAPI.search_exchange_coupon+"/ec_id/"+couponData.dac_coupon_id,
                                            method: 'GET',
            
                                        },
                                        function (err, data) {
                                            //判断是否有错误信息
                                            if (err) {
                                                return cb();
                                            }
                                        
                                            couponInfo = data.data[0];
        
                                            cb();
            
                                        }
                                    );
        

                                }
                            }

                        }
                    }
                );
               
            }else{
                cb();
            }
        },

        function(cb){

            if(!couponInfo){
                cb();
            }else{
                
                //如果有代金劵将代金劵写入用户代金劵表
                if(couponData.dac_coupon_type == 0){  //代金劵

                    R.SEND_HTTP(
                        req,
                        {
                            url    : CORE_APIConfig.coreAPI.addUserCashCoupon,
                            method : 'POST',
                            data:{
                                ucc_uid  : resultData.orderData[0].o_uid,
                                ucc_ccid : couponData.dac_coupon_id
                            }
                        },

                        function (err, data) {

                            cb();

                        }
                    );

                }else if(couponData.dac_coupon_type == 1){ //约课劵

                    R.SEND_HTTP(
                        req,
                        {
                            url    : CORE_APIConfig.coreAPI.add_course_coupon,
                            method : 'POST',
                            data:{
                                ucc_uid : resultData.orderData[0].o_uid,
                                ucc_ccid : couponData.dac_coupon_id
                            }
                        },
                        function (err, data) {
    
                            cb();
    
                        }
                    );

                    
                }else if(couponData.dac_coupon_type == 2){ //兑换卷

                    R.SEND_HTTP(
                        req,
                        {
                            url    : CORE_APIConfig.coreAPI.add_exchange_coupon,
                            method : 'POST',
                            data:{
                                uec_uid : resultData.orderData[0].o_uid,
                                uec_ecid : couponData.dac_coupon_id
                            }
                        },
                        function (err, data) {
    
                            cb();
    
                        }
                    );
                    
                }else {
                    return cb();
                }
            }

        },

        //发送私信
        function(cb){

            //查询用户信息
            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.userSearch + "/u_id/" + resultData.orderData[0].o_uid,
                    method: 'POST',
                    data: {
                        fields: ["u_nickname","openid"].join(",")
                    }
                },
                function (err, data) {
                    console.log(data.data);
                    var userInfo = data.data[0];

                    //发送私信
                    if (userInfo.openid == "" || userInfo.openid == null) {
                        return cb();
                    }


                    userInfo.openid = userInfo.openid.replace(/\*/g,"-"); 
                    if(!couponInfo){
                        cb();
                    }else{
                    R.SEND_HTTP(
                        req,
                        {
                            url: "http://127.0.0.1:3400/weixin/templateMessagesSent",
                            method: 'POST',
                            data: {
                                openid: userInfo.openid,
                                type: "faFangDaiJinQuan",
                                voucherName: couponInfo.cc_name
                            }
                        },
                        function (err, data) {

                            console.log(userInfo)

                            console.log("发送私信：",data);
    
                            return cb();
    
                        }
                    );
                    }
                }
            );

        },

        //要循环
        function(cb){
            ////console.log(resultData.orderInfo);
            if(resultData.orderData[0].o_type == 0 && resultData.orderData[0].o_activity_type == 0){
            var tasks = [];

            resultData.courseData.forEach(function (item, i) {
                ////console.log(item.fic_id);
                tasks.push(function (cb) {
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.search_bulk_purchase_list + "/bpac_oaid/" + item.oc_id,
                            method: 'POST',
                            data: {
                                fields: ["bpac_open_price as oc_new_price"].join(",")
                            }
                        },
                        function (err, data) {
                            if(err){
                                //console.log(err);
                            }
                            var inviteData = data.data;
                            // //console.log(inviteData);
                            item.activityPrice = inviteData;
                            cb();
                        }
                    );

                })
            });
            async.waterfall(tasks, cb);
            }else{
                cb();
            }
        },
        //砍价
        function (cb) {
            if(resultData.orderData[0].o_type == 0 && resultData.orderData[0].o_activity_type == 2){
                ////console.log("art_type" + art_type);
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_organization_activity + "/oa_type/" + resultData.orderData[0].o_activity_type+"/limit/2",
                        method: 'POST',
                        data: {
                            fields: [
                                "oa_id as oc_id", "oa_title as oc_title","oa_title_image as oc_title_image","oa_oid as oc_oid"
                            ].join(",")
                        }
                    },
                    function (err, data) {

                        resultData.courseData = data.data;

                        cb();
                    }
                );
            }else{
                cb();
            }
        },
        //要循环
        function(cb){
            ////console.log(resultData.orderInfo);
            if(resultData.orderData[0].o_type == 0 && resultData.orderData[0].o_activity_type == 2){
                var tasks = [];

                resultData.courseData.forEach(function (item, i) {
                    ////console.log(item.fic_id);
                    tasks.push(function (cb) {
                        R.SEND_HTTP(
                            req,
                            {
                                url: CORE_APIConfig.coreAPI.searchBargainActivityConfig + "/bc_oaid/" + item.oc_id,
                                method: 'POST',
                                data: {
                                    fields: ["bc_least_price as oc_new_price"].join(",")
                                }
                            },
                            function (err, data) {
                                if(err){
                                    //console.log(err);
                                }
                                var inviteData = data.data;
                                // //console.log(inviteData);
                                item.activityPrice = inviteData;
                                cb();
                            }
                        );

                    })
                });
                async.waterfall(tasks, cb);
            }else{
                cb();
            }
        },

    ],function(err, result){
        //返回最终拼接结果
        RES.response(res,true,resultData);

    });

}